package com.ruoyi.web.controller.community.cpc;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.CpcMemberInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.CpcMember;
import com.ruoyi.community.service.ICpcMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 党员管理Controller
 * 
 * @author ruoyi
 * @date 2021-12-15
 */
@RestController
@RequestMapping("/community/cpc/member")
public class CpcMemberController extends BaseController
{
    @Autowired
    private ICpcMemberService cpcMemberService;

    /**
     * 查询党员列表
     */
    @PreAuthorize("@ss.hasPermi('community:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(CpcMember cpcMember)
    {
        startPage();
        List<CpcMember> list = cpcMemberService.selectCpcMemberList(cpcMember);
        return getDataTable(list);
    }

    /**
     * 查询党员及用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('community:member:list')")
    @GetMapping("/listInfo")
    public TableDataInfo listInfo(CpcMember cpcMember)
    {
        startPage();
        List<CpcMemberInfo> list = cpcMemberService.selectCpcMemberInfoList(cpcMember);
        return getDataTable(list);
    }

    /**
     * 查询是否为党员
     */
    @GetMapping(value = "/ifCpc/{id}")
    public boolean ifCpc(@PathVariable("id") String id)
    {
        CpcMember cpcMember = new CpcMember();
        cpcMember.setUserId(Long.valueOf(id));
        cpcMember.setCommunityId(SecurityUtils.getCommunityId());
        List<CpcMember> list = cpcMemberService.selectCpcMemberList(cpcMember);
        if (list.size() > 0)
            return true;
        else
            return false;
    }

    /**
     * 导出党员管理列表
     */
    @PreAuthorize("@ss.hasPermi('community:member:export')")
    @Log(title = "党员管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CpcMember cpcMember)
    {
        List<CpcMember> list = cpcMemberService.selectCpcMemberList(cpcMember);
        ExcelUtil<CpcMember> util = new ExcelUtil<CpcMember>(CpcMember.class);
        return util.exportExcel(list, "党员管理数据");
    }

    /**
     * 获取党员管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('community:member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(cpcMemberService.selectCpcMemberById(id));
    }

    /**
     * 新增党员管理
     */
    @PreAuthorize("@ss.hasPermi('community:member:add')")
    @Log(title = "党员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUser sysUser)
    {
        return toAjax(cpcMemberService.insertCpcMember(sysUser));
    }

    /**
     * 修改党员管理
     */
//    @PreAuthorize("@ss.hasPermi('community:member:edit')")
    @Log(title = "党员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CpcMember cpcMember)
    {
        return toAjax(cpcMemberService.updateCpcMember(cpcMember));
    }

    /**
     * 删除党员管理
     */
    @PreAuthorize("@ss.hasPermi('community:member:remove')")
    @Log(title = "党员管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(cpcMemberService.deleteCpcMemberByIds(ids));
    }
}
